package com.yunfeng.supercall.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.yunfeng.supercall.R;
import com.yunfeng.supercall.app.AppContext;
import com.yunfeng.supercall.bean.Constants;
import com.yunfeng.supercall.bean.UserBean;
import com.yunfeng.supercall.httputils.HttpClient;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;

/**
 * 反馈页面
 * 
 * @author 洋
 * 
 */
public class FeedBackActivity extends Activity implements OnClickListener, Constants{

	private EditText mEditTextSuggestion;
	String phone ;
	private Button mButtonSubmit;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_suggestion);
		UserBean mUser = BaseActivity.getUser(this);
		phone = mUser.getPhone();
		mEditTextSuggestion = (EditText) findViewById(R.id.et_suggestion);

		mFrameLayoutBack = (FrameLayout) findViewById(R.id.framelayout_back);

		mButtonSubmit = (Button) findViewById(R.id.submit);
		mFrameLayoutBack.setOnClickListener(this);
		mButtonSubmit.setOnClickListener(this);
	}

	/**
	 * Submit the info user inputed to the server
	 * 
	 * @param v
	 */
	public void submit() {
		String msg = mEditTextSuggestion.getText().toString();
		if (!AppContext.isMobileNO(phone)) {
			Toast.makeText(getApplicationContext(), getResources().getString(R.string.input_correct_phone_number), Toast.LENGTH_SHORT).show();
			return;
		}
		if (TextUtils.isEmpty(msg)) {
			Toast.makeText(getApplicationContext(), getResources().getString(R.string.input_content), Toast.LENGTH_SHORT).show();
			return;
		}
		JSONObject json = BaseActivity.getBaseParams(FeedBackActivity.this);
		try {
			json.put("i_type", "android");
			json.put("i_content", msg);
			json.put("i_phone", phone);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		showProgressDialog(getResources().getString(R.string.submiting));
		mHttpClient.post(FUNCTION_ADD_FEED_BACK, json, new MyAjaxCallBack() {

			@Override
			public void onReceiveError(String msg, String errorCode) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
				cancelProgressDialog();
			}

			@Override
			public void onReceiveData(String data, String msg) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), getResources().getString(R.string.submit_success), Toast.LENGTH_SHORT).show();
				cancelProgressDialog();
				finish();
			}
		});

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == mButtonSubmit) {
			submit();
		} else if(v == mFrameLayoutBack) {
			finish();
		}
	}


	private ProgressDialog mProgressDialog;

	public void showProgressDialog(String msg) {
		if (mProgressDialog == null) {
			mProgressDialog = new ProgressDialog(this);
			mProgressDialog.setCancelable(false);
		}
		if (TextUtils.isEmpty(msg)) {
			msg = "正在加载数据...";
		}
		mProgressDialog.setMessage(msg);
		mProgressDialog.show();
	}

	public void cancelProgressDialog() {
		if (mProgressDialog != null) {
			mProgressDialog.cancel();
		}
	}
	public HttpClient mHttpClient = new HttpClient();
	private FrameLayout mFrameLayoutBack;
}

